package com.evon.yardmanagement.ui.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.evon.yardmanagement.export.ImportDataManager;
import com.evon.yardmanagement.export.ImportableData;
import com.evon.yardmanagement.export.SectionExportableData;
import com.evon.yardmanagement.service.SectionManagementService;
import com.opensymphony.xwork2.ActionSupport;

public class SectionImportAction extends ActionSupport implements
		ServletRequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File sectionUploadFile;

	private HttpServletRequest servletRequest;

	public String execute() {
		try {
			SectionExportableData data = new SectionExportableData(null);
			data.setFileToImport(sectionUploadFile);
			
			ImportDataManager<ImportableData> importManager = new ImportDataManager<ImportableData>(data);
			importManager.importDataFromExcel();
			
			new SectionManagementService().importSections(data.getData());
			System.err.println("sfsdfsfsfsfsf"+this.sectionUploadFile.getName());
//			String filePath = servletRequest.getSession().getServletContext()
//					.getRealPath("/");
//			System.out.println("Server path:" + filePath);
//			File fileToCreate = new File(filePath, this.userImageFileName);
//			FileUtils.copyFile(this.userImage, fileToCreate);
			
		} catch (Exception e) {
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	public File getSectionUploadFile() {
		return sectionUploadFile;
	}

	public void setSectionUploadFile(File sectionUploadFile) {
		this.sectionUploadFile = sectionUploadFile;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}
}